/* 
 * File:   MyBatchUnit.cpp
 * Author: Morpheus
 * 
 * Created on 20 juin 2013, 16:15
 */


#include "MyBatchUnit.h"
#include <QtCore/qshareddata.h>
#include <QtCore/quuid.h>
#include <QtGui/qtextdocument.h>
#include <QtCore/qfile.h>
#include <QtCore/qdir.h>
#include <QtCore/qfileinfo.h>
#include <QtCore/qiodevice.h>
#include "rtfreader.h"
#include "TextDocumentRtfOutput.h"
#include <QtWidgets/qapplication.h>

const int MyBatchUnit::version = 1;

MyBatchUnitData::MyBatchUnitData()
 : QSharedData(), m_uuid(QUuid::createUuid().toString()), m_document(0) {
}
MyBatchUnitData::MyBatchUnitData(const MyBatchUnitData& orig)
 : QSharedData(orig), m_uuid(orig.m_uuid), m_filePath(orig.m_filePath), m_patern(orig.m_patern), m_document(orig.m_document) {
}
MyBatchUnitData::~MyBatchUnitData(){
    if(m_document)
        m_document->deleteLater();
    m_document = 0;
}

QString MyBatchUnitData::getFilePath() const {
    return m_filePath;
}
bool MyBatchUnitData::isFile() const{
    return  !m_filePath.isEmpty();
}
QFileInfo MyBatchUnitData::getFileInfo() const{
    QDir rootDir(qApp->applicationDirPath());
    return QFileInfo(rootDir.absoluteFilePath(m_filePath));
}

void MyBatchUnitData::setFilePath(const QString& filePath) {
    this->m_filePath = filePath;
}
void MyBatchUnitData::setUUID(const QString& v){
    m_uuid = v;
}
QString MyBatchUnitData::getUUID() const{
    return m_uuid;
}
QString MyBatchUnitData::getContentText() const {
    return m_patern;
}
void MyBatchUnitData::setContentText(const QString& patern) {
    m_patern = patern;
}

QTextDocument* MyBatchUnitData::document() const{
    if(m_document == NULL){
        m_document = new QTextDocument();
        if(isFile()){
            QFileInfo info = getFileInfo();
            QString fmt = info.suffix().toLower();
            if(fmt == "rtf"){
                RtfReader::Reader reader;
                if(reader.open( info.absoluteFilePath() )){
                    RtfReader::TextDocumentRtfOutput output( m_document );
                    reader.parseTo( &output );
                }
            }else if(fmt == "html" || fmt == "htm"){
                QFile f(info.absoluteFilePath());
                if(f.open(QIODevice::ReadOnly | QIODevice::Text)){
                    m_document->setHtml(f.readAll());
                    f.close();
                }
            }else{
                QFile f(info.absoluteFilePath());
                if(f.open(QIODevice::ReadOnly | QIODevice::Text)){
                    m_document->setPlainText(f.readAll());
                    f.close();
                }
            }
        }else{
            m_document->setPlainText(m_patern);
        }
        m_document->setModified(false);
    }
    return m_document;
}

void MyBatchUnitData::save(){
    if(isModified()){
        if(m_filePath.isEmpty()){
            m_patern = m_document->toPlainText();
        }else{
            // sauver le fichier
        }
        m_document->setModified(false);
    }
}
bool MyBatchUnitData::isModified() const{
    return m_document && m_document->isModified();
}

MyBatchUnit::MyBatchUnit()
 : d(new MyBatchUnitData()) {
}

MyBatchUnit::MyBatchUnit(const MyBatchUnit& orig)
 : d(orig.d) {
}

MyBatchUnit::~MyBatchUnit() {
}

void MyBatchUnit::detach(){
    d.detach();
}

bool MyBatchUnit::isFile() const{
    return d->isFile();
}
QFileInfo MyBatchUnit::getFileInfo() const{
    return d->getFileInfo();
}

QString MyBatchUnit::getFilePath() const {
    return d->getFilePath();
}

void MyBatchUnit::setFilePath(const QString& filePath) {
    d->setFilePath(filePath);
}

QString MyBatchUnit::getContentText() const {
    return d->getContentText();
}

void MyBatchUnit::setContentText(const QString& patern) {
    d->setContentText(patern);
}

QTextDocument* MyBatchUnit::document() const{
    return d->document();
}
    
void MyBatchUnit::save(){
    d->save();
}
bool MyBatchUnit::isModified() const{
    return d->isModified();
}

void MyBatchUnit::setUUID(const QString& s) {
    d->setUUID(s);
}

QString MyBatchUnit::getUUID() const{
    return d->getUUID();
}


bool MyBatchUnit::operator==(const MyBatchUnit& obj){
    return d->getUUID() == obj.getUUID();
}


QDataStream& operator<<(QDataStream& s, const MyBatchUnit& obj){
    s << MyBatchUnit::version;
    s << obj.getUUID();
    s << obj.getFilePath();
    s << obj.getContentText();
    return s;
}
QDataStream& operator>>(QDataStream& s, MyBatchUnit& obj){
    QString tmp;        int version = 0;
    s >> version >> tmp;
    obj.d->setUUID(tmp);
    s >> tmp;
    obj.setFilePath(tmp);
    s >> tmp;
    obj.setContentText(tmp);
    return s;
}
